'''
Created on 19 Dec 2011

@author: Dave
'''


import sys
#sys.stdout = open("my_stdout.log","w")
#sys.stderr=open("my_stderr.log","w")

from PyQt4 import QtGui
from PyQt4 import QtCore

import dump



class Photobucket(QtGui.QWidget):
    
    def __init__(self):
        super(Photobucket, self).__init__()
        
        self.initUI()
        
    def initUI(self):

        QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10))
        
        self.setToolTip('<b>dave_the_cave\'s</b> photobucket dump')

        self.vbox = QtGui.QVBoxLayout()


        #button to initiate the dump of photos 
        self.pbtn = QtGui.QPushButton('Photobucket Dump', self)
        self.pbtn.setToolTip('Press to <b>Download</b> the photobucket photos')
        self.pbtn.resize(self.pbtn.sizeHint())
        self.pbtn.clicked.connect(self.downloadAlbum)

        #photobucket album url
        self.album_btn = QtGui.QPushButton('Album', self)
        self.album_btn.setToolTip('Enter the photo album Url')
        self.album_btn.clicked.connect(self.showAlbumDialog)
        
        self.album_url = QtGui.QLineEdit(self)
        self.album_url.setToolTip('Enter the photo album url')

        self.album=QtGui.QHBoxLayout()
        self.album.addWidget(self.album_btn)
        self.album.addWidget(self.album_url)
        self.album.addStretch(1)

        #user id or e-mail
        self.user_id_btn = QtGui.QPushButton('User', self)
        self.user_id_btn.setToolTip('Enter user-id or e-mail')
        self.user_id_btn.clicked.connect(self.showUserDialog)
        
        self.user_id_text = QtGui.QLineEdit(self)
        self.user_id_text.setToolTip('Enter user-id or e-mail')

        self.user_id=QtGui.QHBoxLayout()
        self.user_id.addWidget(self.user_id_btn)
        self.user_id.addWidget(self.user_id_text)
        self.user_id.addStretch(1)

        #password
        self.password_btn = QtGui.QPushButton('Password', self)
        self.password_btn.setToolTip('Enter password')
        self.password_btn.clicked.connect(self.showPasswordDialog)
        
        self.password_text = QtGui.QLineEdit(self)
        self.password_text.setToolTip('Enter password')

        self.password=QtGui.QHBoxLayout()
        self.password.addWidget(self.password_btn)
        self.password.addWidget(self.password_text)
        self.password.addStretch(1)

        #output folder
        self.folder_btn = QtGui.QPushButton('Output Folder', self)
        self.folder_btn.setToolTip('Enter output folder path')
        self.folder_btn.clicked.connect(self.showFolderDialog)
        
        self.folder_text = QtGui.QLineEdit(self)
        self.folder_text.setToolTip('Enter output folder')

        self.folder=QtGui.QHBoxLayout()
        self.folder.addWidget(self.folder_btn)
        self.folder.addWidget(self.folder_text)
        self.folder.addStretch(1)


        #photobucket dump
        self.dump=QtGui.QHBoxLayout()
        self.dump.addStretch(1)
        self.dump.addWidget(self.pbtn)

        self.vbox.addLayout(self.album)
        self.vbox.addLayout(self.user_id)
        self.vbox.addLayout(self.password)
        self.vbox.addLayout(self.folder)
        self.vbox.addStretch(1)
        self.vbox.addLayout(self.dump)

        self.setLayout(self.vbox) 
        self.setGeometry(200, 300, 400, 250)
        self.setWindowTitle('Photobucket Dump')
        self.setWindowIcon(QtGui.QIcon('ui/darkplaces_avatar.jpg')) 
        self.show()

    def downloadAlbum(self):
        print "Downloading album"
        print "user-id","\'"+self.user_id_text.text()+"\'"
        print "password","\'"+self.password_text.text()+"\'"
        print "url","\'"+self.album_url.text()+"\'"
        print "destination","\'"+self.folder_text.text()+"\'"
        dump.download_album(str(self.album_url.text()), str(self.user_id_text.text()), str(self.password_text.text()), str(self.folder_text.text()))
        
    def showAlbumDialog(self):
        
        text, ok = QtGui.QInputDialog.getText(self, 'Input Album url', 
            'Enter your Photobucket url:')
        
        if ok:
            self.album_url.setText(str(text))

    def showUserDialog(self):
        
        text, ok = QtGui.QInputDialog.getText(self, 'Input user-id or e-mail', 
            'Enter your user-id or e-mail:')
        
        if ok:
            self.user_id_text.setText(str(text))

    def showPasswordDialog(self):    
        text, ok = QtGui.QInputDialog.getText(self, 'Input password', 
            'Enter your password:')
        
        if ok:
            self.password_text.setText(str(text))

    def showFolderDialog(self):

        directory = QtGui.QFileDialog.getExistingDirectory(
                      self, "Select output folder for photo download",
                      QtCore.QDir.currentPath(), # Should be pulled out from QSettings
                      QtGui.QFileDialog.ShowDirsOnly
                      )
     
        
        self.folder_text.setText(str(directory))
        
def main():
    
    app = QtGui.QApplication(sys.argv)
    ex = Photobucket()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
